/*pmFWCFu5nhKkBzYmrkBakV0bJk4JW5gQtRRkgMJz728LT5N2JTIvOitt7vMR49l2*/
import fa from 'fa';

import constants from '../constants';
import { doSingleSave, doDeleteLineUnsaved, doDeleteLineSaved } from './buttonClick';

const { fa_components: { ReferFilter: { addBodyReferFilter } } } = fa;
import ampub from 'ampub';

const {
	components: { LoginContext: { getContext, loginContext, loginContextKeys }, faQueryAboutUtils: { openAssetCardByPk } },
	utils: { multiLangUtils: { getMultiLangByID } }
} = ampub;

const {
	ReferFilterConfig,
	UISTATE,
	destDataSource,
	PAGE_CODE,
	AREA: { LIST: { GRID_AREA: tableId } },
	LIST_BTNS: { TABLE_BODY: { SAVE, EXTEND, DELETE, CANCEL } }
} = constants;

//加载模板
export default function(props) {
	const that = this;
	props.createUIDom(
		{
			pagecode: PAGE_CODE.LIST //页面id
		},
		(data) => {
			if (data) {
				if (data.context) {
					// 初始化上下文变量
					loginContext(data.context, destDataSource);

					//初始化时获取后台数据源信息，并存在缓存中，用于解锁
					getContext(loginContextKeys.dataSourceCode, destDataSource);
				}

				if (data.button) {
					let button = data.button;
					props.button.setButtons(button);
					// 行删除时悬浮框提示
					/* 国际化处理： 确定要删除吗？*/
					props.button.setPopContent('Delete', getMultiLangByID('msgUtils-000001'));
				}
				if (data.template) {
					let meta = data.template;
					that.templateid = meta.pageid;
					modifierMeta.call(that, props, meta);
					props.meta.setMeta(meta);

					props.cardTable.setStatus(tableId, UISTATE.EDIT);
				}
			}
		}
	);
}

//模板修改
function modifierMeta(props, meta) {
	const that = this;
	let status = props.getUrlParam('status');

	addBodyReferFilter.call(this, props, meta, ReferFilterConfig);

	//表体添加操作列
	let oprCol = {
		attrcode: 'opr',
		label: getMultiLangByID('amcommon-000000') /* 国际化处理： 操作*/,
		visible: true,
		itemtype: 'customer', //操作列的制定类型
		className: 'table-opr',
		width: 150,
		fixed: 'right',
		render(text, record, index) {
			return (
				<div>
					{// 创建saga状态控制按钮
					props.button.createErrorButton({
						record,
						sucessCallBack: () => {
							let { values: { pk_card: { value: pk_card } } } = record;
							let buttonAry = [];
							if (pk_card) {
								//表示已经保存了卡片， 展示的按钮： 展开、删除
								buttonAry = [ EXTEND, DELETE ];
							} else {
								//表示未保存卡片， 展示的按钮： 保存、取消
								buttonAry = [ SAVE, CANCEL ];
							}
							return props.button.createOprationButton(buttonAry, {
								area: 'card_body_inner',
								ignoreHotkeyCode: [ SAVE ],
								buttonLimit: 3,
								onButtonClick: (props, key) => tableButtonClick.call(that, props, key, text, record, index, 'card_body_inner')
							});
						}
					})}
				</div>
			);
		}
	};
	meta[tableId].items.map((item) => {
		//使用状况、增加方式只能选择末级
		if (item.attrcode === 'pk_addreducestyle' || item.attrcode === 'pk_usingstatus') {
			item.onlyLeafCanSelect = true;
		}
	});
	meta[tableId].items.push(oprCol);
	return meta;
}

function tableButtonClick(props, key, text, record, index) {
	const that = this;

	switch (key) {
		//跳转到卡片编辑页
		case EXTEND:
			let { values: { pk_card: { value: pk_card } } } = record;
			if (pk_card) {
				//跳转到卡片界面进行编辑
				openAssetCardByPk(props, pk_card, { status: UISTATE.EDIT });
			}
			break;

		//删除卡片
		case DELETE:
			doDeleteLineSaved(that, index);
			break;

		//保存卡片
		case SAVE:
			doSingleSave(that, index);
			break;

		//取消
		case CANCEL:
			doDeleteLineUnsaved(that, index);
			break;

		default:
			break;
	}
}

/*pmFWCFu5nhKkBzYmrkBakV0bJk4JW5gQtRRkgMJz728LT5N2JTIvOitt7vMR49l2*/